//
//  NewFeatureViewCell.swift
//  TakeTime
//
//  Created by bachelor on 16/11/5.
//  Copyright © 2016年 bachelor. All rights reserved.
//

import UIKit

class NewFeatureViewCell: UICollectionViewCell {
    
    private lazy var iconView: UIImageView = {
        return UIImageView()
    }()
    // 开始按钮
    private lazy var startButton: UIButton = {
        let btn = UIButton()
        btn.setTitle("开始体验", for: UIControlState.normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), for: UIControlState.normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), for: UIControlState.highlighted)
        btn.addTarget(self, action: #selector(NewFeatureViewCell.btnClick), for: .touchUpInside)
        
        return btn
    }()
    
    var imageIndex: Int = 0 {
        didSet {
            iconView.image = UIImage.init(named: "new_feature_\(imageIndex + 1)")
            startButton.isHidden = true
        }
    }
    // 按钮点击切换控制器
    func btnClick() {
        NotificationCenter.default.post(name: Notification.Name(rawValue: ZSwitchVCNotification), object: false)
    }
    
    func showStartBtn() {
        startButton.isHidden = false
        startButton.transform = CGAffineTransform.init(scaleX: 0, y: 0)
        startButton.isUserInteractionEnabled = false
        
        UIView.animate(withDuration: 0.5, delay: 0, options: .init(rawValue: 0), animations: {
            
            self.startButton.transform = CGAffineTransform.identity
            
        }, completion: {_ in
            self.startButton.isUserInteractionEnabled = true
        })
        
        
    }
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        addSubview(iconView)
        addSubview(startButton)
        
        iconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[subView]|", options: .alignAllLastBaseline, metrics: nil, views: ["subView": iconView]))
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[subView]|", options: .alignAllLastBaseline, metrics: nil, views: ["subView": iconView]))
        startButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: startButton, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraints([NSLayoutConstraint(item: startButton, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: -200)])
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        iconView.frame = self.bounds
    }
}
